import Dep, { pushTarget, popTarget } from "./Dep";

let id = 0;
class Watcher {

    constructor(vm,exprOrFn,cb,options){
        this.vm = vm;
        this.exprOrFn = exprOrFn;
        this.cb = cb;
        this.options = options;
        this.id = id++;
        this.deps = [];// watcher记录有多少dep依赖
        this.depsId = new Set();
        if(typeof exprOrFn == 'function'){
            this.getter = exprOrFn;
        }
        this.get();
    }
    get(){
        
       pushTarget(this);
        this.getter();
        popTarget();
    }
    update(){
        this.get();
    }
    addDep(dep){
        let id = dep.id;
        if(!this.depsId.has(id)){
            this.deps.push(dep);
            this.depsId.add(id);
            dep.addSub(this); 
        }
    }
}



export default Watcher;